define(["app","js/services/MuseService",'js/services/MessageBox'],
    function (app) {
        var deps = ["$scope", "$http", "$rootScope", "$filter", "$state", "$stateParams","MuseService","MessageBox"];

        function controller($scope, $http, $rootScope, $filter, $state, $stateParams,MuseService,MessageBox) {
            $scope.curUser = getSessionJson('curUserObj');

            if(isEmpty($scope.curUser)){
                var url = 'login.html';
                location.replace(url);
            }

            $scope.driver = {};
            // 是否编辑
            $scope.isEdit = false;

            $scope.getDriverDetail = function() {
                MuseService.post("driver/getDriverDetailByUser/"+$scope.curUser.id, {

                }).success(function(data) {
                    if (data.code == 0) {
                        $scope.driver = data.data;
                    } else {
                        MessageBox.error("查询失败:" + data.msg);
                    }
                });
            };

            $scope.getDriverDetail();

            // 保存发现 基本信息
            $scope.saveObj = function() {
                MuseService.postObj("driver/saveDriver/"+$scope.curUser.id , $scope.driver).success(function(data) {
                    if (data.code == 0) {
                        MessageBox.notify("保存成功！");
                        history.back();
                    } else {
                        MessageBox.error("保存失败: " + data.msg);
                    }
                });
            };


            //绑定车辆页面
            $scope.openCarDetail = function(id){
                //跳转前先判断是否有车辆再审核中

                MuseService.post("car/car/"+id, {
                    id : id
                }).success(function(data) {
                    if (data.code == 0) {
                        if(data.data && data.data.isEffect && data.data.isEffect == 'check'){
                            MessageBox.error("你的车辆审核中!请稍后");
                            return;
                        }

                        $state.go("cardetail", {
                            "id" : id
                        });

                    } else {
                        MessageBox.error("查询失败:" + data.msg);
                    }
                });
            }


            $scope.delDriver = function(){
                MuseService.post("check/driverpushdel", {id:$scope.driver.id}).success(function(data) {
                    if (data.code == 0) {
                        MessageBox.notify("保存成功！");
                        history.back();
                    } else {
                        MessageBox.error("保存失败: " + data.msg);
                    }
                });
            }

            // 取消，返回
            $scope.close = function () {
                history.back();
            };
        }

        controller.$inject = deps;
        app.lazy.controller("DriverDetailController", controller);
    });